<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="onSearch()" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="收费日期">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="tjlbbh" label="统计类别" class="w-72">
          <SelectItem v-model:value="formState.tjlbbh" type="DT00311" placeholder="请选择" allow-clear />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="flex-1">
      <a-space ref="exportRef" class="flex justify-end mb-2">
        <a-button @click="onPrint()">打印</a-button>
        <BaseExport
          type="报表"
          name="门诊费用按类别统计"
          code="000840"
          is-show-time
          :search-params="{ ...formState, startDate: formState?.dateRange?.[0], endDate: formState?.dateRange?.[1] }"
          >导出</BaseExport
        >
      </a-space>
      <BaseTable :loading="loading" :data-source="tableData" fill column-code="000840" :extra-height="50">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onOpenDetail(record)">明细</a-button>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell />
              <a-table-summary-cell>合计</a-table-summary-cell>
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(amountTatol.ybje, 2) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(amountTatol.zfje, 2) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(amountTatol.zje, 2) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell class="text-left">
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
    <Detail v-model:open="openDetail" :row-data="rowData" :form-state="formState" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import { formatAmount } from '@/utils/format.js'
import Detail from './Detail.vue'

const exportRef = ref(null)
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  tjlbbh: ''
})
const openDetail = ref(false)
const rowData = ref({})
const onSearch = () => {
  getData()
  getAmount()
}
const { loading, tableData, getData } = useTable({
  url: '/statistic/getCategoryStatistics',
  params: formState.value,
  pagination: false,
  immediate: true,
  formatParams: (params) => {
    return {
      ...params,
      startDate: params?.dateRange?.[0],
      endDate: params?.dateRange?.[1]
    }
  }
})
const amountTatol = ref({})
const getAmount = async () => {
  const res = await post('/statistic/getCategoryStatisticsTj', {
    startDate: formState.value?.dateRange?.[0],
    endDate: formState.value?.dateRange?.[1],
    tjlbbh: formState.value?.tjlbbh
  })
  if (res.code === 0) {
    amountTatol.value = res.data
  }
}
//打印
const onPrint = async () => {
  const { code, msg, data } = await post('/statistic/printCategoryStatistics', {
    startDate: formState.value?.dateRange?.[0],
    endDate: formState.value?.dateRange?.[1],
    tjlbbh: formState.value?.tjlbbh
  })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}

const onOpenDetail = (record) => {
  openDetail.value = true
  rowData.value = record
}
onMounted(() => {
  getAmount()
})
</script>
